<template>
  <div class="pass">
    <!-- 通行证管理 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>突发事件管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <div class="sousuo">
        <div>
          <!-- 一个输入框，两个按钮 -->
          <el-button type="info" @click="getEventAdd">新建</el-button>
          <el-button type="info" @click="dorefresh">刷新</el-button>
        </div>

        <div class="search">
          <!-- 一个输入框，两个按钮 -->
          <el-input v-model="searchById" placeholder="请输入事件ID" />
          <el-button type="primary" @click="getEventById">搜索</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
        </div>
      </div>

      <!-- 表格 -->

      <el-table :data="eventList">
        <!-- <el-table-column label="选择">
          <el-button @click="getEventAdd()">添加事件</el-button>
        </el-table-column> -->
        <!-- 快速复制某行 alt+shift+剪头 -->
        <el-table-column prop="master.id" label="事件ID"></el-table-column>
        <el-table-column prop="base.uname" label="住户"></el-table-column>
        <el-table-column prop="master.time" label="登记时间"></el-table-column>
        <el-table-column prop="master.title" label="事件名称"></el-table-column>

        <!-- 只有当状态为待审核，也就是0时，才展示两个按钮 -->

        <el-table-column label="操作" width="400px">
          <template v-slot="scope">
            <div class="operation">
              <el-button
                type="primary"
                :plain="true"
                @click="detail(scope.row.master.message)"
                >详情</el-button
              >
              <!-- <el-button color="red">发布公告</el-button>
              <el-button color="red">上门处理</el-button> -->
              <el-button
                color="green"
                @click="
                  gotoupdate(
                    scope.row.master.id,
                    scope.row.master.time,
                    scope.row.master.title,
                    scope.row.master.message
                  )
                "
                >编辑</el-button
              >
              <el-button color="red" @click="deleteEvent(scope.row.master.id)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="paging"
        background
        layout="prev, pager, next"
        :total="1000"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

  <script >
// getAllPassAPi获取所有通行证 发请求
import { ElMessage } from "element-plus";
import {
  getAllPassAPi,
  deletePassApi,
  changePassStatusApi,
  getUserPassApi,
} from "../api/index.js";
import {
  getAllEventAPI,
  deleteEventApi,
  getEventByIdApi,
} from "../api/index1.js";

export default {
  inject: ["refresh"],
  data() {
    return {
      passList: [],
      searchById: "",
      eventList: [],
    };
  },
  methods: {
    //9.测试刷新
    dorefresh() {
      this.refresh();
    },
    // 8、go to update
    gotoupdate(id, time, title, message) {
      // 3.跳转，隐式传递参数 只能用name
      this.$router.push({
        // path:'../views/noticeUpdate',
        name: "eventUpdate",
        params: {
          id: id,
          time: time,
          title: title,
          message: message,
        },
      });
    },
    detail(msg) {
      // console.log(msg)
      ElMessage({
        message: msg,
        grouping: true,
        type: "success",
      });
    },
    getEventAdd() {
      this.$router.push({
        path: "../views/eventAdd", //要跳转的页面的路由
      });
    },
    // 1.获取通行证列表的方法
    // getPassList() {
    //   getAllPassAPi().then((res) => {
    //     // console.log(res)
    //     this.passList = res.data.data;
    //     // console.log(this.passList)
    //   });
    // },

    getEventList() {
      getAllEventAPI().then((res) => {
        // console.log(res);
        this.eventList = res.data;
        // console.log(this.eventList)
      });
    },
    // 2.根据id删除通行证
    deleteEvent(id) {
      deleteEventApi(id).then((res) => {
        // console.log(res);
        // 1.刷新数据
        this.getEventList();
        // 2.提示用户删除成功
        if (res.code == 20000) {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
        }
      });
    },
    // 3.改变通行证状态
    changeStatus(id, status) {
      // console.log(id + status);

      let obj = {
        id: id,
        status: status,
      };
      changePassStatusApi(obj).then((res) => {
        // console.log(res);
        if (res.code == 20000) {
          // this.getPassList();
          ElMessage({
            message: "更新成功",
            type: "success",
          });
        }
      });
    },
    // 4.搜索
    getEventById() {
      getEventByIdApi(this.searchById)
        .then((res) => {
          console.log(res);
          if (res.code == 20000) {
            this.eventList = res.data;
          }
        })
    },
    // 5.重置
    reset() {
      this.searchById = "";
      this.getEventList();
    },
    //6.test方法
    test() {
      testApi().then((res) => {
        // console.log(res)
      });
    },
  },
  created() {
    // this.getPassList();
    this.getEventList();
  },
};
</script>

  <style lang="less" scoped>
.pass {
  .el-breadcrumb {
    margin-bottom: 30px;
  }
}
.search {
  width: 300px;
  display: flex;

  .el-input {
    margin-right: 30px;
  }
}

.operation {
  display: flex;
}

.paging {
  display: flex;
  justify-content: right;
}

.sousuo {
  display: flex;
  justify-content: space-between;
}
</style>
